﻿using ServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Search : Page
{


    public static List<Airport> airports = null;


    protected void Page_Load(object sender, EventArgs e)
    {

        ServiceReference.ServiceClient service = new ServiceReference.ServiceClient();
        airports = service.GetAll(typeof(Airport).Name).OfType<Airport>().ToList();
        service.Close();
    }



    protected void SearchBtn_Click(object sender, EventArgs e)
    {


        ServiceReference.ServiceClient service = new ServiceReference.ServiceClient();
        List<ServiceReference.Airport> airportsToPrint = new List<ServiceReference.Airport>();


        int stops;
        Session["Pasagers"] = Convert.ToInt32(PassagersDropDownList.SelectedValue);

        Airport fromAirport = getAirportByName(FromTextBox.Text);
        Airport toAirport = getAirportByName(ToTextBox.Text);
        DateTime dt = new DateTime();
        if (IsPostBack)
        {
            string dd = departureDate.Text;
            departureDate.Text = dd;
            dt = DateTime.ParseExact(dd, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
        }

        string criteria = CriteriaDropDownList.SelectedItem.Value;
        if (EscalesDropDownList.SelectedIndex == 3)
            stops = 10;
        else
            stops = EscalesDropDownList.SelectedIndex;

        int noOfPersons = Convert.ToInt32(PassagersDropDownList.SelectedValue);

        IList<ServiceReference.PathOfAirportScheduleL4OapnGX> result = service.searchForFlight(fromAirport, toAirport, criteria, dt, noOfPersons, stops);
        
        Session["result"] = result;
        int l = result.Count;
        Session["noResult"] = l;

        int index = 0;
        
        foreach (ServiceReference.PathOfAirportScheduleL4OapnGX path in result)
        {
            index++;

            foreach (ServiceReference.EdgeOfAirportScheduleL4OapnGX edge in path.Edges)
            {
                Session["BookingLine" + index] = path;
                fromAirport = edge.From.Object;
                toAirport = edge.To.Object;

                TableRow tr = new TableRow();


                TableCell td = new TableCell();
                td.CssClass = "rowStyle";
                td.Text = "Departure: " + edge.Data.startTime.ToString();
                tr.Cells.Add(td);

                td = new TableCell();
                td.CssClass = "rowStyle";
                td.Text = fromAirport.name;
                tr.Cells.Add(td);


                td = new TableCell();
                td.CssClass = "rowStyle";
                td.Text = "Arival:" + edge.Data.endTime.ToString();
                tr.Cells.Add(td);

                td = new TableCell();
                td.Text = toAirport.name;
                td.CssClass = "rowStyle";

                tr.Cells.Add(td);
                td = new TableCell();
                td.Text = edge.Data.price.ToString();
                td.CssClass = "rowStyle";
                

                tr.Cells.Add(td);
                td = new TableCell();
                td.Text = service.getFreeSeats(edge.Data.scheduleId).ToString();
                td.CssClass = "rowStyle";

                tr.Cells.Add(td);

           

                
                if (index == 1)
                    results1.Rows.Add(tr);
                else if (index == 2)
                    results2.Rows.Add(tr);
                else if (index == 3)
                    results3.Rows.Add(tr);
            }


        }

    }


    public void BookBtn1_Click(object sender, EventArgs e)
    {
        
        Session["ChosenPath"] = 1;
        Server.Transfer("~/Booking.aspx");
    }
    public void BookBtn2_Click(object sender, EventArgs e)
    {
       
        Session["ChosenPath"] = 2;
        Server.Transfer("~/Booking.aspx");
    }
    public void BookBtn3_Click(object sender, EventArgs e)
    {
        
        Session["ChosenPath"] = 3;
        Server.Transfer("~/Booking.aspx");
    }

    public string getAirportName(int i)
    {
        var arrayData = (ServiceReference.Airport[])Session["ArrayData"];
        return arrayData[i].name;

    }

    public Airport getAirportByName(string name)
    {
        Airport airport = null;
        IEnumerator<Airport> enumerator = airports.GetEnumerator();
        bool found = false;
        while(enumerator.MoveNext() && !found)
        {
            if(enumerator.Current.name.Equals(name))
            {
                found = true;
                airport = enumerator.Current;
            }
        }
        return airport;
    }


    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetCompletionList(string prefixText, int count)
    {

        List<string> airportsList = new List<string>();

        foreach(Airport airport in airports)
        {
            if (airport.name.StartsWith(prefixText, StringComparison.OrdinalIgnoreCase)) airportsList.Add(airport.name);
        }
        

        return airportsList;
    }




}